#ifndef GAME_H
#define GAME_H

// Libraries
#include <irrlicht.h>
#include "singleton.h"
#include "network.h"

// Namespaces
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

// Forward Declarations
class CState;
class SingleNetworkClass;
class MultiNetworkClass;

class CGame: public Singleton<CGame>
{
public:
	bool Init(int argc, char **argv);
	bool Close();

	void Run();

	// States
	bool RegisterState(stringc _name,CState* _state);
	bool SwitchState(stringc _stname);
	bool PushState(stringc _stname);
	bool PopState();
	bool PopAll();

	CState* CurrentState();

	void Exit() { m_NeedExit = true; }

	// Networking
	bool IsLocalServerRunning() const { return LocalServerRunning; }
	void StartLocalServer();
	void StopLocalServer();

	void SendPacketToHost(PacketClass *TPacket);
	bool Connect(const char *TIPAddress);
	void Disconnect();
	void WaitForDisconnect();

private:
	void Delay(int TTime);
	void ResetTimer();
	void ResetGraphics();

	irr::core::map<stringc,CState*> m_StatesMap;
	list<CState*>		 m_StateStack;

	// States
	bool PreviousWindowActive, WindowActive;

	enum GameType
	{
		GT_DDS,
		GT_MAPEDITOR,
		GT_CLIENT,
		GT_CONNECT
	};

	GameType m_GameType;

	// Flags
	bool m_NeedExit;
	bool MouseWasLocked;
	bool MouseWasShown;

	// Time
	u32 TimeStamp, DeltaTime;

	// Networking
	bool LocalServerRunning;

	ENetHost *Connection;
	ENetPeer *Peer;
	ENetPeer SinglePeer;
};

#endif
